package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.ProductComment;
import com.ericom.service.GenericManager;

public class ProductCommentAction extends BaseAction {

	private GenericManager<ProductComment, Long> productCommentManager;
	private List productComments;
	private ProductComment productComment;
	private Long id;

	public ProductComment getProductComment() {
		return productComment;
	}

	public void setProductComment(ProductComment productComment) {
		this.productComment = productComment;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setProductCommentManager(
			GenericManager<ProductComment, Long> productCommentManager) {
		this.productCommentManager = productCommentManager;
	}

	public String list() {
		productComments = productCommentManager.getAll();
		return SUCCESS;
	}

	public List getProductComments() {
		return productComments;
	}

	public String delete() {
		productCommentManager.remove(productComment.getId());
		this.saveMessage(getText("productComment.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			productComment = productCommentManager.get(id);
		} else {
			productComment = new ProductComment();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (productComment.getId() == null);
		productComment = productCommentManager.save(productComment);

		String key = (isNew) ? "productComment.added"
				: "productComment.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
